package com.sz.app.oss.api.logistics.base;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.base.service.ShippingLineSettingService;
import com.sz.biz.logistics.base.dto.ShippingLineSettingDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.StringUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Function: ShippingLineSettingController <br>
 * Author: wentao.chang <br>
 * Date: 2017-08-04 15:19:00
 */
@RestController
@RequestMapping("${api}/biz/shipinglinesettings")
@Api(description = " ", tags = "026-05、海运航班")
public class ShippingLineSettingController extends BaseController {
    @Autowired
    private ShippingLineSettingService shippingLineSettingService;


    @ApiOperation(value = "添加海运航班", notes = "添加海运航班, <br>权限:" + Permissions.BIZ_MGT_BASE_LINE_SHIPPING_SET)
    @RequestMapping(value = "/", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_LINE_SHIPPING_SET)
    public ResultDto createShippingLineSettingModel(@RequestBody @ApiParam(value = "海运航班", required = true) ShippingLineSettingDto model) {
        int shipsettingid = shippingLineSettingService.save(model);
        ResultDto dto = this.getSaveSuccessDto(MessagesCodes.MODULE_SHIPPINGLINESET);
        dto.setData(shipsettingid);
        return dto;
    }

    @ApiOperation(value = "修改海运航班", notes = "修改海运航班<br>权限：" + Permissions.BIZ_MGT_BASE_LINE_SHIPPING_SET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "海运航班Id", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "model", value = "海运航班", required = true, dataType = "ShippingLineSettingDto", paramType = "body")
    })
    @RequestMapping(value = "/{id}", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_LINE_SHIPPING_SET)
    public ResultDto updateShippingLineSettingModel(@PathVariable("id") int id, @RequestBody ShippingLineSettingDto model) {
        shippingLineSettingService.update(id, model);
        return this.getUpdateSuccessDto(MessagesCodes.MODULE_SHIPPINGLINESET);
    }

    @ApiOperation(value = "批量删除海运航班", notes = "批量删除海运航班<br>权限：" + Permissions.BIZ_MGT_BASE_LINE_SHIPPING_SET)
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_LINE_SHIPPING_SET)
    public ResultDto deleteShippingLineSettingModels(@RequestBody @ApiParam(value = "要删除的id集合", required = true) List<Integer> ids) {
        shippingLineSettingService.deleteByIds(ids);
        return this.getDeleteSuccessDto(MessagesCodes.MODULE_SHIPPINGLINESET);
    }

    @ApiOperation(value = "获取海运航班", notes = "获取海运航班<br>权限：")
    @ApiImplicitParam(name = "id", value = "海运航班Id", required = true, dataType = "int", paramType = "path")
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    public ResultDto getShippingLineSettingModel(@PathVariable("id") int id) {
        ShippingLineSettingDto dto = shippingLineSettingService.findById(id);
        return new ResultDto(dto);
    }

    @ApiOperation(value = "获取海运航班", notes = "获取海运航班<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startPortId", value = "起运港id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "endPortId", value = "目的港id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "q", value = "查询关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "起始时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "抵达时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "lineId", value = "航线id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "升序", dataType = "boolean", paramType = "query")
    })
    @RequestMapping(value = "/", method = {RequestMethod.GET})
    public ResultDto getShippingLineSettingModels(@RequestParam(required = false) String q,
                                                  @RequestParam(required = false) Integer startPortId,
                                                  @RequestParam(required = false) Integer endPortId,
                                                  @RequestParam(required = false) String startTime,
                                                  @RequestParam(required = false) String endTime,
                                                  @RequestParam(required = false) Integer lineId,
                                                  @RequestParam(defaultValue = "1") int pageIndex,
                                                  @RequestParam(defaultValue = "20") int pageSize,
                                                  @RequestParam(required = false) String sort,
                                                  @RequestParam(defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        return shippingLineSettingService.findShippingLineSettingListPage(new QueryParams(pageSize, pageIndex, q, sort, asc)
                .put("startPortId", startPortId)
                .put("endPortId", endPortId)
                .put("startTime", StringUtils.isEmpty(startTime) ? startTime : startTime + " 00:00:00")
                .put("endTime", StringUtils.isEmpty(startTime) ? endTime : endTime + " 00:00:00")
                .put("lineId", lineId)
        );
    }

    @ApiOperation(value = "获取海运航班短接口", notes = "获取海运航班短接口<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineId", value = "航线的id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "q", value = "查询关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "页码", dataType = "int", paramType = "query"),
    })
    @RequestMapping(value = "/short", method = {RequestMethod.GET})
    public ResultDto getShippingLineSettingModels(@RequestParam(required = false) int lineId,
                                                  @RequestParam(required = false) String q,
                                                  @RequestParam(defaultValue = "1") int pageIndex,
                                                  @RequestParam(defaultValue = "20") int pageSize) {
        q = StringUtils.replaceSqlPattern(q);
        QueryParams qp = new QueryParams(pageSize, pageIndex, q, null, true);
        qp.put("shippingLineId", lineId);
        return shippingLineSettingService.findShippingLineSettingShortListPage(qp);
    }

    @ApiOperation(value = "检查海运航班编码是否存在", notes = "检查海运航班编码是否存在<br>权限：" + Permissions.BIZ_MGT_BASE_LINE_SHIPPING_SET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "code", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/checkcode", method = {RequestMethod.GET})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_LINE_SHIPPING_SET)
    public ResultDto checkCodeExist(@RequestParam(required = false) String code,
                                    @RequestParam(required = false) Integer id) {
        shippingLineSettingService.checkCode(code, id);
        return new ResultDto();
    }
}
